// pages/opreation/form/index.js
import {
  request
} from '../../../../request';
import WxValidate from "../../../../utils/WxValidate";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      commodityNumber: 0,
    }
  },
  onLoad: function (options) {
    this.initValidate();
    if (options.id != undefined) {
      this.loadFormData(options.id);
    }
  },
  loadFormData(id) {
    request("/operation/commodity/" + id, "GET").then(res => {
      if (res.code == 200) {
        this.setData({
          form: res.data
        })
      }
    })
  },
  initValidate() {
    let rules = {
      commodityName: {
        required: true
      },
      commodityPrice: {
        required: true,
        number: true,
      }
    }

    let message = {
      commodityName: {
        required: '请输入商品名称'
      },
      commodityPrice: {
        required: '请输入商品价格',
        number: '请输入正确的价格'
      }
    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  onChange(e) {
    var formData = this.data.form;
    formData.commodityNumber = e.detail.value;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  formSubmit: function (e) {
    console.log(e);
    var formData = this.data.form;
    let params = e.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      let error = this.WxValidate.errorList[0];
      switch (error.param) {
        case "commodityName":
          //TODO
          break;
        case "commodityPrice":
          //TODO
          break;
      }
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 2000,
      })
      return false;
    }
    if (params.commodityName != "") {
      formData["commodityName"] = params.commodityName;
    }
    if (params.phone != "") {
      formData["phone"] = params.phone;
    }
    var status;
    if (formData.commodityId != undefined) {
      status = "PUT";
    } else {
      status = "POST";
    }
    request("/operation/commodity", status, formData).then(res => {
      if (res.code == "200") {
        wx.showToast({
          title: '保存成功',
          icon: '',
          duration: 2000,
        })
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: res.msg+"",
          icon: 'none',
          duration: 2000,
        })
      }
    })
    console.log(formData);
  },
  noteInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.remark = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  priceInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.commodityPrice = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
  nameInput:function(e){
    var content = e.detail.value;
    var formData = this.data.form;
    formData.commodityName = content;
    this.setData({
      form: formData
    })
    console.log(this.data);
  },
})